<!DOCTYPE html>
<%@ page language="java" errorPage="/error.jsp" pageEncoding="UTF-8" contentType="text/html;charset=utf-8"%>
<html>
<head>
<title>组织机构管理</title>
<%@include file="/common/head.jsp" %>
<link href="<%=request.getContextPath()%>/static/plugin/zTree/css/zTreeStyle/zTreeStyle.css" rel="stylesheet">
<script src="<%=request.getContextPath()%>/static/plugin/zTree/js/jquery.ztree.all-3.5.min.js"></script>
<style>
	#content {
        position: absolute;
        top: 0;
        left: 240px;
        right: 10px;
        bottom: 0;
        box-sizing: border-box;
    }
    ul.ztree {
       width: 230px;
    }
</style>
</head>
<body class="iframe-page">
    <div class="tree-wrap">
    <div class="tree-title">组织机构列表</div>
    <div class="tree-content">
	    <ul id="orgList" class="ztree"></ul>
	</div>
	</div>
	
	
	<div id="content">
  		<iframe id="orgFrame" frameborder="0" style="width: 100%;height: 100%"></iframe>
	</div>

	<div id="orgMenu" style="width:120px;">
	     <div id="add" iconCls="icon-create">新建子节点</div>
	     <div id="edit" iconCls="icon-edit">编辑节点</div>
	     <div id="delete" iconCls="icon-delete">删除节点</div>
	</div>
<script>
	var orgFrame = document.getElementById('orgFrame'),
		currentNode = "",
		ztree;
	$(function() {
		initTree('orgList',ORG_CONST_URL.LIST,'code','pCode');
		
		initRightMenu("orgMenu");
	});
	
	// 加载组织机构树
	function  initTree(treeId,url,id,pId) {
		var setting = {
			data: {
				simpleData: {
					enable: true,
					idKey: id,
					pIdKey: pId
				}
			},
			async: {
				enable: true
			},
			edit: {
				enable: false,
				drag: {
					isMove: false
				}
			},
			callback: {
				onClick: viewOrg,
				onRightClick: showOrgMenu
			}
		};
		
		$.sajax({
			url: url + '?random=' + Math.random() * 1000000,
			onLoadSuccess: function(data) {
				ztree = $.fn.zTree.init($('#' + treeId),setting,data.data);
				expandNode();
			}
		});
	}
	
	 // 展开树节点
    function expandNode() {
       var nodeCode = '${param.expandCode}',
           node;
       if (nodeCode) {
           // 获取需要自动展开的节点
           node = ztree.getNodeByParam('code', nodeCode, null);
       } else {
           // nodeId为空,展开根节点
           node = ztree.getNodeByParam('pCode', null, null);
       }
       // 如果该节点不包含子节点,则展开其父节点
       if (node && !node.isParent) {
          node = node.getParentNode(); 
       }
       ztree.expandNode(node, true, false, true);
    }
	
	
	//初始化右键菜单
	function initRightMenu(menuId) {
		$('#' + menuId).menu({
            onClick: function(item){
            	if (item.id == 'add') {
            		addOrg(currentNode);
            	} else if (item.id == 'edit') {
            		editOrg(currentNode);
            	} else if (item.id == 'delete') {
            		deleteOrg(currentNode);
            	}
            }
        }); 
	}
	
	//新建
	function addOrg(node) {
		orgFrame.src = ORG_CONST_URL.INIT_ADD + "?id=" + node.id;
		//选中节点
		ztree.selectNode(node);
	}
	
	//编辑
	function editOrg(node) {
		orgFrame.src = ORG_CONST_URL.INIT_EDIT + "?id=" + node.id;
		ztree.selectNode(node);
	}
	
	//删除
	function deleteOrg(node) {
		$.sdialog({
     		type: 'confirm',
     		msg: '您确定要删除<'+node.name+'>节点吗? 删除时会将该节点下的子节点一并删除',
     		onConfirm: function(result) {
     			if(result){
     				$.sajax({
     	                url: ORG_CONST_URL.DEL+'?id='+node.id,
     	                onLoadSuccess: function (data) {
                            $.stip({
                            	type:'ok',//显示类型 有info、 ok、 danger
             	                text: '删除成功！',
             	                onBeforeHide: function () {
             	                	refreshList(node.pCode);
             	                }
                            });
     	                }
     	            });
     			}
     		}
     	});
	}
	
	//查看
	function viewOrg(event, treeId, treeNode) {
		var url = ORG_CONST_URL.INIT_VIEW + "?id=" + treeNode.id;
		if(!treeNode.pId) {
			url = ORG_CONST_URL.INIT_VIEW + "?id=" + treeNode.id;
		}
		orgFrame.src = url;
	}
	
	//显示右键菜单
	function showOrgMenu(event, treeId, treeNode) {
		//点击节点时显示右键菜单，防止没有点击节点时出现右键菜单
		if(treeNode) {
			currentNode = treeNode;
			//显示和隐藏删除
			var item = $('#orgMenu').menu('getItem', $('#delete')[0]);
            if(!currentNode.pCode) {
                $('#orgMenu').menu('disableItem', item.target);
            } else {
            	$('#orgMenu').menu('enableItem', item.target);
            }
            //右键菜单显示
			$('#orgMenu').menu('show',{left: event.pageX, top: event.pageY});
			
		}
	}
	
	// 刷新页面
	function refreshList(expandCode, isContinue) {
		var url = ORG_CONST_URL.INIT_MAIN ;
		// 继续添加
		if (isContinue) {
			var node = ztree.getNodeByParam("code", expandCode, null);
            ztree.setting.async.url = ORG_CONST_URL.GET_TREE + "?pCode=" + expandCode;
            node.isParent = true;
            ztree.updateNode(node);
            ztree.reAsyncChildNodes(node, "refresh");
		} else {
		   window.location.href = url + '?expandCode=' + expandCode;
		}
	}
	
	//请求url
	var ORG_CONST_URL = {
		INIT_MAIN: basePath + "/sys/org/init", 
	    LIST: basePath + "/sys/org/list",
	    GET_TREE: basePath + "/sys/org/getTree",
	    INIT_ADD: basePath + "/sys/org/initAdd",
	    INIT_EDIT: basePath + "/sys/org/initEdit",
	    INIT_VIEW: basePath + "/sys/org/initView",
	    DEL: basePath + "/sys/org/delete"
	};
</script>
</body>
</html>
